package com.xl.processor;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;

public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        System.out.println("beanDefinitionMap填充完毕后回调该方法");
        //修改某个BeanDefinition
        //BeanDefinition beanDefinition = beanFactory.getBeanDefinition("userService");
        //beanDefinition.setBeanClassName("com.xl.dao.impl.UserDaoImpl");
        //动态注册一个BeanDefinition
        BeanDefinition beanDefinition = new RootBeanDefinition();
        beanDefinition.setBeanClassName("com.xl.dao.impl.PersonDaoImpl");
        //强转成DefaultListableBeanFactory，DefaultListableBeanFactory是属于ConfigurableListableBeanFactory 的子类。
        //在Spring中，ConfigurableListableBeanFactory 是一个接口，用于配置和操作可列举的bean工厂。
        //而defaultListableBeanFactory是ConfigurableListableBeanFactory的默认实现类，
        //它是Spring容器中默认的bean工厂对象，用于管理所有的bean实例。
        DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) beanFactory;
        defaultListableBeanFactory.registerBeanDefinition("personDao",beanDefinition);
    }
}
